package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelCommendQuery;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommendService commendService;
    @PostMapping
    public JSONResult save(Travel travel){
        //设置作者
        travel.setAuthor(UserContext.getCurrentUser());
        travelService.save(travel);
        return new JSONResult();
    }
    @PutMapping("/{id}")
    public JSONResult update(Travel travel){
        travel.setAuthor(UserContext.getCurrentUser());
        travelService.update(travel);
        return new JSONResult();
    }
    @GetMapping("/{id}")
    public Travel getTravel(@PathVariable Long id){
        return travelService.getById(id);
    }
    @GetMapping
    public PageInfo listTravels(TravelQuery qo){
        //已发布
        qo.setState(Travel.STATE_RELEASE);
        //公开
        qo.setIsPublic(true);
        //发布时间倒序
        qo.setOrderBy("t.releaseTime desc");
        return travelService.query(qo);
    }
    @GetMapping("/commends")
    public PageInfo listTravelCommends(TravelCommendQuery qo){
        //推荐时间倒序
        qo.setOrderBy("schedule desc");
        return commendService.queryForApp(qo);
    }
}
